/**
 * Table.java
 * Copyright (C) 2011 Yuanyan<yuanyan.cao@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact yuanyan.cao@gmail.com
 */
package edu.rabbit.kernel.table;

import java.io.ByteArrayInputStream;
import java.io.InputStream;


import edu.rabbit.Database;
import edu.rabbit.DbErrorCode;
import edu.rabbit.DbException;
import edu.rabbit.ValueType;
import edu.rabbit.kernel.DbUtility;
import edu.rabbit.kernel.memory.IMemoryPointer;
import edu.rabbit.table.ICursor;
import edu.rabbit.table.ITransaction;

/**
 * Base implementation of {@link ICursor}.
 * 
 * @author Yuanyan<yanyan.cao@gmail.com>
 * 
 * 
 */
public abstract class Cursor implements ICursor {

    protected final IBtreeTable btreeTable;
    protected final Database db;

    Cursor(IBtreeTable table, Database db) throws DbException {
        if (db.isInTransaction()) {
            this.btreeTable = table;
            this.db = db;
        } else {
            throw new DbException(DbErrorCode.MISUSE, "Cursor requires active transaction");
        }
    }

    public void close() throws DbException {
        db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                btreeTable.close();
                return null;
            }
        });
    }

    public boolean eof() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.eof();
            }
        });
    }

    public boolean first() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.first();
            }
        });
    }

    public boolean last() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.last();
            }
        });
    }

    public boolean next() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.next();
            }
        });
    }

    public boolean previous() throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.previous();
            }
        });
    }

    public int getFieldsCount() throws DbException {
        return (Integer) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.getFieldsCount();
            }
        });
    }

    public ValueType getFieldType(final int field) throws DbException {
        return (ValueType) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.getFieldType(field);
            }
        });
    }

    public boolean isNull(final int field) throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.isNull(field);
            }
        });
    }

    public String getString(final int field) throws DbException {
        return (String) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.getString(field);
            }
        });
    }

    public long getInteger(final int field) throws DbException {
        return (Long) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.getInteger(field);
            }
        });
    }

    public double getFloat(final int field) throws DbException {
        return (Double) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.getFloat(field);
            }
        });
    }

    public byte[] getBlobAsArray(final int field) throws DbException {
        return (byte[]) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                IMemoryPointer buffer = btreeTable.getBlob(field);
                return buffer != null ? DbUtility.readByteBuffer(buffer) : null;
            }
        });
    }

    public InputStream getBlobAsStream(final int field) throws DbException {
        return (InputStream) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                IMemoryPointer buffer = btreeTable.getBlob(field);
                return buffer != null ? new ByteArrayInputStream(DbUtility.readByteBuffer(buffer)) : null;
            }
        });
    }

    public Object getValue(final int field) throws DbException {
        return db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                Object value = btreeTable.getValue(field);
                if (value instanceof IMemoryPointer) {
                    return new ByteArrayInputStream(DbUtility.readByteBuffer((IMemoryPointer) value));
                }
                return value;
            }
        });
    }

    public boolean getBoolean(final int field) throws DbException {
        return (Boolean) db.runReadTransaction(new ITransaction() {
            public Object run(Database db) throws DbException {
                return btreeTable.getInteger(field) != 0;
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.tmatesoft.rabbit.core.table.ICursor#reverse()
     */
    public ICursor reverse() throws DbException {
        return new ReverseOrderCursor(this);
    }

}
